const promise_state = {
    pending: 0
    , fulfilled: 1
    , rejected: 2
}
class newPromise {
    #result
    #state = promise_state.pending // pending 0 fulfilled 1 rejected 2
    #callback = []
    constructor(executor) {
        executor(this.#resolve, this.#reject)//调用回调函数
    }
    #resolve = (value) => {
        if (this.#state !== promise_state.pending) return
        this.#result = value
        this.#state = promise_state.fulfilled //表示数据填充成功了

        /*

            从数据结构上,我们这个处理依旧是在直接执行函数,然后将其丢进栈中,而不是放进我们的微任务队列里所以要将其放弃微队列中去!
            我们解决不能同时调用多个then的问题,问题导致的原因是因为调用的过程中被重复赋值了,导致前一个回调函数被覆盖,从而导致不能读取到多个then的结果.
            解决的方法是,我们使用一个数组进行存储,再利用遍历数组去进行各个调用从而实现读取多个then
        */
        queueMicrotask(() => {
            this.#callback.forEach((cb) => {
                cb()

            })
        })
    }
    #reject(reason) { }
    then(onFulfilled, onRejected) {
        if (this.#state === promise_state.pending) {

            this.#callback.push(() => {
                onFulfilled(this.#result)
            })

        }
        else if (this.#state === promise_state.fulfilled) {
            queueMicrotask(() => { onFulfilled(this.#result) })
        }
    }

}
const mp = new newPromise((resolve, reject) => {
    setTimeout(() => {
        resolve("GG")
    }, 1000)
})
console.log(mp);
mp.then((result) => {
    console.log("读取数据", result);
})
mp.then((result) => {
    console.log("读取数据2", result);
})